# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=glpk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.0
pkgrel=5
pkgdesc="GNU Linear Programming Kit: solve LP, MIP and other problems (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64' 'mingw32')
url="https://www.gnu.org/software/glpk/glpk.html"
msys2_references=(
  'archlinux: glpk'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://ftp.gnu.org/gnu/glpk/glpk-${pkgver}.tar.gz"
        'timeval-64bit.patch'
        'fix-platform-check.patch'
        '0003-no-embedded-zlib.patch'
        '0004-no-embedded-suitesparse.patch')
sha256sums=('4a1013eebb50f728fc601bdd833b0b2870333c3b3e5a816eeba921d95bec6f15'
            '2d09d58139fd85d695f5b2933ec750adf0b438a1e9a53dee1b264b80dd2e0f6a'
            '9a866c1e14f7a200a46b83c78b9f2d207167633e6ef0c51341008f07ee677d8a'
            '54acbe1f9f0561adcc1d0052b1175918004b7168352d126e23da499df564a42c'
            '71c040ab77e64756ac29203714d2b94f994354cedefe5f5111f07fab6ff5e626')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare () {
  cd "${srcdir}/glpk-${pkgver}"

  apply_patch_with_msg \
    timeval-64bit.patch \
    fix-platform-check.patch \
    0003-no-embedded-zlib.patch \
    0004-no-embedded-suitesparse.patch

  sed -i "s|-version-info|-no-undefined -version-info|g" src/Makefile.am

  autoreconf -vfi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --with-gmp \
    --with-gnu-ld

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
